package com.ai.service.made.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ai.common.CommonResponse;
import com.ai.entity.BasePage;
import com.ai.entity.cameraFunctions.CameraFlowCollectionPlatformPage;
import com.ai.entity.cameraFunctions.CameraFlowCollectionPlatformParam;
import com.ai.entity.made.CameraFlowCollectionPlatform;
import com.ai.entity.made.RegionBase;
import com.ai.mapper.made.CameraFlowCollectionPlatformMapper;
import com.ai.model.vo.CameraFlowCollectionPlatformVo;
import com.ai.service.made.ICameraFlowCollectionPlatformService;
import com.ai.service.made.IRegionBaseService;
import com.ai.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2024-08-27
 */
@Service
public class CameraFlowCollectionPlatformServiceImpl extends ServiceImpl<CameraFlowCollectionPlatformMapper, CameraFlowCollectionPlatform> implements ICameraFlowCollectionPlatformService {

    @Resource
    private IRegionBaseService iRegionBaseService;

    @Override
    public CommonResponse addNewPlat(CameraFlowCollectionPlatformParam cameraFlowCollectionPlatformParam) {

        CameraFlowCollectionPlatform cameraFlowCollectionPlatform = new CameraFlowCollectionPlatform();
        BeanUtils.copyProperties(cameraFlowCollectionPlatformParam,cameraFlowCollectionPlatform);
        LocalDateTime dateTime = LocalDateTime.now();
        cameraFlowCollectionPlatform.setCreateTime(dateTime);
        cameraFlowCollectionPlatform.setUpdateTime(dateTime);
        cameraFlowCollectionPlatform.setCreateUser(SecurityUtils.getLoginUserName());
        cameraFlowCollectionPlatform.setUpdateUser(SecurityUtils.getLoginUserName());

        save(cameraFlowCollectionPlatform);

        return CommonResponse.ok("新增成功");
    }

    @Override
    public CommonResponse getPlatInfo(String id) {

        CameraFlowCollectionPlatform collectionPlatform = getById(id);

        return CommonResponse.ok(collectionPlatform);
    }

    @Override
    public CommonResponse editPlatInfo(CameraFlowCollectionPlatformParam cameraFlowCollectionPlatformParam) {

        CameraFlowCollectionPlatform collectionPlatform = getById(cameraFlowCollectionPlatformParam.getId());
        collectionPlatform.setPlatformName(cameraFlowCollectionPlatformParam.getPlatformName());
        collectionPlatform.setHost(cameraFlowCollectionPlatformParam.getHost());
        collectionPlatform.setPort(cameraFlowCollectionPlatformParam.getPort());
        collectionPlatform.setAppKey(cameraFlowCollectionPlatformParam.getAppKey());
        collectionPlatform.setAppSecret(cameraFlowCollectionPlatformParam.getAppSecret());
        collectionPlatform.setRegionBaseId(cameraFlowCollectionPlatformParam.getRegionBaseId());

        this.updateById(collectionPlatform);

        return CommonResponse.ok("更新成功");
    }

    @Override
    public CommonResponse delPlatInfo(String id) {

        this.removeById(id);

        return CommonResponse.ok("删除成功");
    }

    @Override
    public CommonResponse pageList(CameraFlowCollectionPlatformPage platformPage) {

        BasePage<CameraFlowCollectionPlatformVo> cameraFlowCollectionPlatformVoBasePage = new BasePage<>();

        QueryWrapper<CameraFlowCollectionPlatform> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        platformPage.getQuery(queryWrapper);
        IPage<CameraFlowCollectionPlatform> iPage = page(platformPage, queryWrapper);
        List<CameraFlowCollectionPlatform> records = iPage.getRecords();
        List<CameraFlowCollectionPlatformVo> collect = new LinkedList<>();
        if(CollectionUtil.isNotEmpty(records)){
            collect = records.stream().map(platform -> {
                CameraFlowCollectionPlatformVo cameraFlowCollectionPlatformVo = new CameraFlowCollectionPlatformVo();
                BeanUtils.copyProperties(platform, cameraFlowCollectionPlatformVo);
                try {
                    RegionBase regionBase = iRegionBaseService.getById(platform.getRegionBaseId());
                    cameraFlowCollectionPlatformVo.setRegionName(regionBase.getRegionName());
                } catch (Exception e) {
                    log.error("当前取流平台未分配组织");
                }
                return cameraFlowCollectionPlatformVo;
            }).collect(Collectors.toList());
        }

        BeanUtils.copyProperties(iPage,cameraFlowCollectionPlatformVoBasePage);
        cameraFlowCollectionPlatformVoBasePage.setRecords(collect);

        return CommonResponse.ok(cameraFlowCollectionPlatformVoBasePage);
    }

    @Override
    public CommonResponse gePlatList() {

        List<String> belowRegionIdList  =  iRegionBaseService.getBelowRegionIdList(SecurityUtils.getLoginRegionBaseId());

        List<CameraFlowCollectionPlatform> list = list(new QueryWrapper<CameraFlowCollectionPlatform>().in("region_base_id",belowRegionIdList));

        //创建从摄像头取流实例-----取流实例id固定-1
        CameraFlowCollectionPlatform platform = new CameraFlowCollectionPlatform();
        platform.setId(-1);
        platform.setPlatformName("摄像机取流");
        list.add(platform);

        CameraFlowCollectionPlatform platform2 = new CameraFlowCollectionPlatform();
        platform2.setId(-2);
        platform2.setPlatformName("本地视频取流");
        list.add(platform2);

        return CommonResponse.ok(list);
    }

    @Override
    public List<Integer> getBelowPlatId(String regionBaseId) {
        List<String> belowRegionIdList =  iRegionBaseService.getBelowRegionIdList(SecurityUtils.getLoginRegionBaseId());

        List<CameraFlowCollectionPlatform> cameraFlowCollectionPlatforms =
                list(new QueryWrapper<CameraFlowCollectionPlatform>().in("region_base_id", belowRegionIdList));
        //获取所有的平台id
        if (CollectionUtil.isNotEmpty(cameraFlowCollectionPlatforms)){
            List<Integer> belowPlatIdList = cameraFlowCollectionPlatforms.stream().map(CameraFlowCollectionPlatform::getId).collect(Collectors.toList());
            return belowPlatIdList;
        }

        return null;
    }

}
